/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webcamtracking;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author James
 */
public class Grid {

    private ArrayList<GridElement> list;
    private int width;
    private int height;
    private int workingID;

    public Grid(int width, int height) {
        this.width = width;
        this.height = height;
        list = new ArrayList<GridElement>();
        for (int i = 0; i < (width * height); i++) {
            list.add(new GridElement());
        }
        workingID = 1;
    }

    public GridElement get(int x, int y) {
        int index = x + (y * (width));
        if (index < list.size()) {
            return list.get(index);
        }
        return list.get(list.size() - 1);
    }

    public void set(int x, int y, GridElement e) {
        list.set(x + (y * width), e);
    }

    public void add(GridElement e) {
        list.add(e);
    }

    public GridElement[][] toArray() {
        group();
        GridElement[][] array = new GridElement[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                array[x][y] = get(x, y);
            }
        }
        return array;
    }

    @Override
    public String toString() {
        group();
        String str = "";
        Iterator<GridElement> it = list.iterator();
        int count = 0;
        while (it.hasNext()) {
            if (count == width) {
                count = 0;
                str += "\n";
            }
            str += it.next().getId();
            count++;
        }
        return str;
    }

    public void group() {
        workingID =1;
                
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> group = new ArrayList<Integer>();
            if (list.get(i).isActive()) {                
                groupAdd(group, i);
                Iterator<Integer> it = group.iterator();
                while(it.hasNext()){
                    list.get(it.next()).setId(workingID);
                }
                workingID++;                
            }
            
        }
    }
    
    private void groupAdd(ArrayList<Integer> group, int index) {
        if (list.get(index).isActive()&&
                !list.get(index).isChecked()) {
            list.get(index).setChecked(true);
            group.add(index);
            int[] neighbours = getNeighbours(index);
            for (int i = 0; i < neighbours.length; i++) {
                if (neighbours[i] != -1){
                    groupAdd(group, neighbours[i]);
                }               
            }
        }
    }
    
    

    private int[] getNeighbours(int index) {

        int[] array = new int[4];
        array[0] = getUp(index);
        array[1] = getDown(index);
        array[2] = getLeft(index);
        array[3] = getRight(index);

        return array;
    }

    private int getUp(int index) {
        int i = index - width;
        if (i >= 0) {
            return i;
        } else {
            return -1;
        }
    }

    private int getDown(int index) {
        int i = index + width;
        if (i < list.size()) {
            return i;
        } else {
            return -1;
        }
    }

    private int getLeft(int index) {
        int i = index - 1;
        //if i greater than 0 and index is not a multiple of width
        if (i > 0 && index%width != 0) { //checks if its on the same line
                return i;
        } else {
            return -1;
        }
    }

    private int getRight(int index) {
        int i = index + 1;
        //if i less than list size and index is not a multiple of width
        if (index < list.size() - 1 && i%width != 0) { //checks if its on the same line
            return i;
        } else {
            return -1;
        }
    }

   
    
}
